package com.massa.caronafacil.activities;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import com.massa.caronafacil.R;
import com.massa.caronafacil.R.id;
import com.massa.caronafacil.R.layout;
import com.massa.caronafacil.R.string;
import com.massa.caronafacil.fragments.CaronasTabFragment;
import com.massa.caronafacil.fragments.MapaTabFragment;
import com.massa.caronafacil.fragments.PerfilTabFragment;
import com.massa.caronafacil.view.custom.CustomViewPager;
import com.massa.caronafacil.viewpager.PagerAdapter;

/**
 * The <code>TabsViewPagerFragmentActivity</code> class implements the Fragment activity that
 * maintains a TabHost using a ViewPager.
 */
public class TabsViewPagerFragmentActivity extends FragmentActivity implements
        TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener{

    /** Holds the tag to identify the "tab" when the screen orientation changes. */
    private static final String TAB_TAG = "tab";

    /** For log purpose. */
    private static final String TAG = TabsViewPagerFragmentActivity.class.getSimpleName();

    private static final String FIRST_TAB_TAG = "tab1";

    private static final String SECOND_TAB_TAG = "tab2";

    private static final String THIRD_TAB_TAG = "tab3";

    /** */
    private TabHost mTabHost;

    /** Holds the ViewPager instance. */
    private CustomViewPager mViewPager;

    /** */
    private HashMap<String, TabInfo> mMapTabInfo = new HashMap<String, TabsViewPagerFragmentActivity.TabInfo>();

    /** */
    private PagerAdapter mPagerAdapter;

    /**
     * TabInfo class.
     */
    private class TabInfo {
        /** */
        private String mTag;

        /** */
        private Class<?> mClss;

        /** */
        private Bundle mArgs;

        /** */
        private Fragment mFragment;

        /**
         *
         * @param tag
         * @param clazz
         * @param args
         */
        TabInfo(String tag, Class<?> clazz, Bundle args) {
            mTag = tag;
            mClss = clazz;
            mArgs = args;
        }
    }

    /**
     * A simple factory that returns dummy views to the Tabhost.
     */
    class TabFactory implements TabContentFactory {

        private final Context mContext;

        /**
         * @param context
         */
        public TabFactory(final Context context) {
            mContext = context;
        }

        @Override
        public View createTabContent(final String tag) {
            final View view = new View(mContext);
            view.setMinimumWidth(0);
            view.setMinimumHeight(0);
            return view;
        }

    }

    @Override
    protected final void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Inflate the layout
        setContentView(R.layout.tabs_viewpager_layout);

        // Initialise the TabHost
        initialiseTabHost(savedInstanceState);

        // Intialise ViewPager.
        intialiseViewPager();

        if (savedInstanceState != null) {
            if (mTabHost != null) {
                // Set the tab as per the saved state.
                mTabHost.setCurrentTabByTag(savedInstanceState.getString(TAB_TAG));
            }
        }

        if (savedInstanceState == null) {
            mTabHost.setCurrentTabByTag(FIRST_TAB_TAG);
        }
    }

    /* (non-Javadoc)
     * @see android.support.v4.app.FragmentActivity#onDestroy()
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected final void onSaveInstanceState(final Bundle outState) {
        if (mTabHost != null) {
            // Saves the tab selected.
            outState.putString(TAB_TAG, mTabHost.getCurrentTabTag());
        }

        super.onSaveInstanceState(outState);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    /**
     * Initialises ViewPager.
     */
    private void intialiseViewPager() {
        final List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, CaronasTabFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, MapaTabFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, PerfilTabFragment.class.getName()));

        mPagerAdapter = new PagerAdapter(getSupportFragmentManager(), fragments);

        mViewPager = (CustomViewPager) findViewById(R.id.viewpager);

        if (mViewPager != null) {
            mViewPager.setAdapter(mPagerAdapter);
            mViewPager.setOnPageChangeListener(this);
            mViewPager.setOffscreenPageLimit(fragments.size());
        }
    }

    /**
     * Initialises the Tab Host.
     */
    private void initialiseTabHost(final Bundle args) {
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);

        if ((mTabHost != null) && (mMapTabInfo != null)) {
            mTabHost.setup();

            TabInfo tabInfo = new TabInfo(FIRST_TAB_TAG, CaronasTabFragment.class, args);
            addTab(this,
                    mTabHost,
                    mTabHost.newTabSpec(FIRST_TAB_TAG).setIndicator(getString(R.string.caronasTab)),
                    (tabInfo));

            mMapTabInfo.put(tabInfo.mTag, tabInfo);

            tabInfo = new TabInfo(SECOND_TAB_TAG, MapaTabFragment.class, args);
            addTab(this,
                    this.mTabHost,
                    this.mTabHost.newTabSpec(SECOND_TAB_TAG).setIndicator(
                            getString(R.string.mapaTab)), (tabInfo));
            mMapTabInfo.put(tabInfo.mTag, tabInfo);

            tabInfo = new TabInfo(THIRD_TAB_TAG, PerfilTabFragment.class, args);
            addTab(this,
                    this.mTabHost,
                    this.mTabHost.newTabSpec(THIRD_TAB_TAG).setIndicator(
                            getString(R.string.perfilTab)), (tabInfo));
            mMapTabInfo.put(tabInfo.mTag, tabInfo);

            // Default to first tab.
            mTabHost.setOnTabChangedListener(this);
        }
    }

    /**
     * Add Tab content to the Tabhost.
     *
     * @param activity
     * @param tabHost
     * @param tabSpec
     * @param mClss
     * @param mArgs
     */
    private static void addTab(final TabsViewPagerFragmentActivity activity, final TabHost tabHost,
            final TabHost.TabSpec tabSpec, final TabInfo tabInfo) {
        // Attach a Tab view factory to the spec
        tabSpec.setContent(activity.new TabFactory(activity));
        tabHost.addTab(tabSpec);
    }

    @Override
    public final void onTabChanged(final String tag) {
        // TabInfo newTab = this.mapTabInfo.get(tag);
        final int pos = this.mTabHost.getCurrentTab();
        mViewPager.setCurrentItem(pos);
    }

    @Override
    public final void onPageScrolled(final int position, final float positionOffset,
            final int positionOffsetPixels) {
        // Does nothing.
    }

    @Override
    public final void onPageSelected(final int position) {
        mTabHost.setCurrentTab(position);
    }

    @Override
    public final void onPageScrollStateChanged(final int state) {
        // Does nothing.
    }
}